package com.shenhan.volatiledemo;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

/**
 * @Author shenhan
 * @Date: 2020/6/12 22:34
 * @Description: 不保证原子性
 */
public class Test02 {
    public volatile static AtomicInteger num = new AtomicInteger();

    public static void add() {
//        num++;
        num.getAndIncrement();
    }

    public static void main(String[] args) {
        IntStream.rangeClosed(1,20).forEach(z-> new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                add();
            }
        }).start());

        while (Thread.activeCount() > 2) {
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName() + " " + num);

    }
}
